package com.atlassian.uwc.ui.listeners;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.Vector;

import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileSystemView;

import com.atlassian.uwc.ui.UWCGuiModel;
import com.atlassian.uwc.ui.UWCUserSettings.Setting;

/**
 * adds pages when triggered by an event
 */
public class AddPagesListener extends PageHandler implements ActionListener, MouseListener {

	
	/**
	 * creates the listener object for adding pages to the model
	 * @param ui pane containing the visual rep of the pages
	 * @param model uwc model maintaining the data concerning pages
	 */
	public AddPagesListener(JScrollPane ui, UWCGuiModel model) {
		super(ui, model);
	}

	/**
	 * launches the add pages dialog, and adds the pages to the model and the visible panel
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent arg0) {
		runAddPagesDialog();
	}

	/**
	 * launches the add pages dialog, gets the pages, and updates the ui
	 */
	private void runAddPagesDialog() {
		Vector<String> filenames = addWikiPages();
		JList pagelist = new JList(filenames);
		this.updateUI(this.ui, pagelist);
	}

	/**
	 * lauches the add pages dialog, and gets the pages 
	 * @return vector of filenames corresponding to the pages the user has chosen with the dialog
	 */
	private Vector<String> addWikiPages() {
		//get current dir
    	File currentDir = new File(this.model.getPageChooserDir());
    	
    	//create file chooser
    	JFileChooser chooser = new JFileChooser(currentDir, FileSystemView.getFileSystemView());
        chooser.setMultiSelectionEnabled(true);
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setToolTipText("Ctrl-Tab to cycle through dialog. Escape to cancel. Enter to Choose.");
        chooser.updateUI();

        //open the dialog
        int returnVal = chooser.showOpenDialog(this.ui);
        Vector<String> pages = new Vector<String>();
        
        //get the choice, and add the pages
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File[] files = chooser.getSelectedFiles();
            pages = this.model.addWikiPages(files);
        }
        
        //save the new chooser dir
        String newChooserDir = chooser.getCurrentDirectory().getAbsolutePath();
		this.model.saveSetting(Setting.PAGE_CHOOSER_DIR, newChooserDir);
		
		return pages;
	}

	/**
	 * runs the add pages dialog if the user clicked on the scrollpane or doubleclicked the JList.
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	public void mouseClicked(MouseEvent event) {
		Component parent = event.getComponent();
		//if it's a jscrollpane, or it's a jList while at the same time is a double click...
		if (isJScrollpane(parent)
				|| (isJList(parent) && isDoubleClick(event))
			) 
		{
			runAddPagesDialog(); //then launch the dialog
		}
		
	}

	/**
	 * @param event given event generated by the user's actions
	 * @return true if user double clicked
	 */
	private boolean isDoubleClick(MouseEvent event) {
		return event.getClickCount() > 1;
	}

	/**
	 * true if the given parent component is a JList object
	 * @param parent
	 * @return
	 */
	private boolean isJList(Component parent) {
		return parent instanceof JList;
	}

	/**
	 * true if the given parent component is a jScrollPane object
	 * @param parent
	 * @return
	 */
	private boolean isJScrollpane(Component parent) {
		return parent instanceof JScrollPane;
	}

	/* Mouse Event methods that aren't being used */
	
	public void mouseEntered(MouseEvent arg0) {
		//XXX not using 
	}

	public void mouseExited(MouseEvent arg0) {
		//XXX not using
	}

	public void mousePressed(MouseEvent arg0) {
		//XXX not using
	}

	public void mouseReleased(MouseEvent arg0) {
		//XXX not using
	}

}
